(ns adder.dbManagement
  (:require [clojure.java.jdbc :as sql]))


(def db {:classname "com.mysql.jdbc.Driver"
         :subprotocol "mysql"
         :subname "//localhost:3306/commics"
         :user "admin"
         :password "admin"})

;(defn list-comics []
 ; (sql/with-connection db
  ;  (sql/with-query-results rows
   ;   ["select * from manga"]
      ;(println rows)
    ;  )))




(defn list-one-comic-from-series [id sid]
  (flatten (sql/with-connection db
    (sql/with-query-results row
      ["select * from manga where id = ? and series =?" id] 
      (doall row)))))



;(defn save-commic )

;COMMIC CRUDS
;moze samo :name

(defn list-all-comics-from-serie [sid]
  (flatten (sql/with-connection db
    (sql/with-query-results row
      ["select * from manga where series =?" sid] 
      (doall row)))))

(defn list-comics []
  (flatten (sql/with-connection db
    (sql/with-query-results rows
      ["select * from manga"]
      (doall rows)))))

(defn list-one-comic [id]
  (flatten (sql/with-connection db
    (sql/with-query-results row
      ["select * from manga where id = ?" id] 
      (doall row)))))

(defn insert-manga [name writter location rank series]
  (sql/with-connection db (sql/insert-values :manga [:name :writter :location :rank :series] [name writter location rank series])))

(defn update-manga [id update-map]
  (sql/with-connection db
    (sql/update-values :manga ["id=?" id] update-map)))

(defn delete-manga [id]
  (sql/with-connection db 
    (sql/delete-rows :manga ["id=?" id])))

(defn load-image-path [id]
  (:location (first (sql/with-connection db
    (sql/with-query-results row
      ["select location from manga where id = ?" id] 
      (doall row))))))
;;END

;;SERIES CRUDS
(defn list-one-series [id]
  (flatten (sql/with-connection db
    (sql/with-query-results row
      ["select * from series where Series_ID = ?" id] 
      (doall row)))))

;(defn save-commic )


(defn list-series []
  (flatten (sql/with-connection db
    (sql/with-query-results rows
      ["select * from series"]
      (doall rows)))))

(defn insert-serie [series_name series_publisher]
  (sql/with-connection db (sql/insert-values :series [:series_name :series_publisher] [series_name series_publisher])))

(defn update-serie [id update-map]
  (sql/with-connection db
    (sql/update-values :series ["Series_ID=?" id] update-map)))

(defn delete-series [id]
  (sql/with-connection db 
    (sql/delete-rows :series ["Series_ID=?" id])))